package com.summer.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.summer.reggie.common.R;
import com.summer.reggie.entity.User;
import com.summer.reggie.service.UserService;
import com.summer.reggie.utils.SMSUtils;
import com.summer.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 作者:夏天
 * qq:1205232048
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 发送手机短信验证码
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String > sendMsg(@RequestBody User user, HttpSession session){
        //获取手机号
        String phone = user.getPhone();
        if(StringUtils.isNotEmpty(phone)){
            //生成一个随机四位验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("验证码为: {}",code);

            //调用阿里云提供的短信服务API来完成发送短信
            //SMSUtils.sendMessage("瑞吉外卖","",phone,"validateCode");

            //保存生成的验证码, 存在session中
            //session.setAttribute(phone, code);

            //将生成的验证码存入到 redis中  存活时间设置为5分钟
            redisTemplate.opsForValue().set(phone, code, 5, TimeUnit.MINUTES);

            return R.success("手机短信验证码发送成功!");
        }
        return R.error("短信发送失败!");
    }

    /**
     * 用户登录
     * @param map
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpSession session){
        //获取手机号
        String phone = map.get("phone").toString();
        //获取验证码
        String code = map.get("code").toString();

        //从session中取出验证码
        //Object codeInSession = session.getAttribute(phone);

        //从redis中取出验证码
        Object codeInSession = redisTemplate.opsForValue().get(phone);

        //比对验证码 比对成功则说明登录成功
        if(codeInSession != null && codeInSession.equals(code)){
            //判断手机号对应的用户是否为新用户, 如果是新用户则直接进注册
            LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.eq(User::getPhone,phone);
            User user = userService.getOne(userLambdaQueryWrapper);
            if(user == null){
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            session.setAttribute("user",user.getId());
            //登录成功后, 我们就将redis缓存中的验证码删除
            redisTemplate.delete(phone);
            return R.success(user);
        }
        return R.error("登录失败!");
    }
}
